// https://www.shadertoy.com/view/WlsyR2  Random Grid Shenanigans
// https://www.shadertoy.com/view/XtBfzz

const RandomGridShader = {
  shaderID: 'RandomGridShader',

  uniforms: {
    'iTime': { value: 1.0 },
    'modelHeight': { value: 1.0 }, // 模型高度，约定模型的原点位于底部投影的中心
    'modelWidth': { value: 1.0 },
    'modelDepth': { value: 1.0 },
  },

  vertexShader: /* glsl */`
  varying float height;
  varying float width;
  varying float deep;
  uniform float modelHeight;
  uniform float modelWidth;
  uniform float modelDepth;

  void main() {
    height = position.y + modelHeight * 0.5;
    width = position.x + modelWidth * 0.5;
    deep = position.z + modelDepth * 0.5;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  }`,

  fragmentShader: /* glsl */`
  #define PI 3.14
  #define TARGET_COUNT 15
  #define GRID_CELL_SIZE 0.1
  #define RED vec3(1.0,0.0,0.0)
  #define GREEN vec3(0.0,1.0,0.0)
  #define BLUE vec3(0.0,0.0,1.0)
  
	uniform float iTime;
  uniform float modelHeight;
  uniform float modelWidth;
  uniform float modelDepth;
  varying float height;
  varying float width;
  varying float deep;
  
  const float N = 40.0; // grid ratio
  // --- unfiltered grid ---

  float gridTexture( in vec3 p ) {
    // coordinates
    vec3 i = step( fract(p), vec3(1.0/N) );
    //pattern
    return (1.0-i.x)*(1.0-i.y)*(1.0-i.z);   // grid (N=10)
    
    // other possible patterns are these
    //return 1.0-i.x*i.y;           // squares (N=4)
    //return 1.0-i.x-i.y+2.0*i.x*i.y; // checker (N=2)
  }
  
  void main() {
    vec3 uv = vec3(height / modelHeight, width / modelWidth, deep / modelDepth);
    float minVal = min(min(modelHeight, modelWidth), modelDepth);
    uv *= vec3(modelHeight, modelWidth, modelDepth) / minVal;
    float a = gridTexture(uv * N);
    gl_FragColor = vec4(vec3(a), 1.0);
    
    // vec3 gridBoundUV = uv / GRID_CELL_SIZE;
    // vec3 cellBoundUV = gridBoundUV - round(gridBoundUV);
    //
    // float redIntensity = 0.0;
    // float blueIntensity = 0.0;
    //
    // for (int targetIndex = 0; targetIndex < TARGET_COUNT; ++targetIndex) {
    //     float f_targetIndex = float(targetIndex);
    //
    //     float trigOffset = (PI / float(TARGET_COUNT)) * f_targetIndex;
    //     vec3 targetPosition = vec3(sin(iTime + trigOffset) * 0.51 + tan(f_targetIndex + trigOffset), cos(iTime + trigOffset) * 0.1 + sin(f_targetIndex + trigOffset),
    //       sin(iTime + trigOffset) * 0.1 + cos(f_targetIndex + trigOffset));
    //     vec3 edgeBoundPosition = targetPosition / GRID_CELL_SIZE;
    //
    //     float distanceToTarget = length(gridBoundUV - round(edgeBoundPosition)) + length(gridBoundUV - edgeBoundPosition);
    //     redIntensity += length(GRID_CELL_SIZE / (distanceToTarget * 9.5 * cellBoundUV) ) * GRID_CELL_SIZE; 
    // }
    //
    // for (int targetIndex = 0; targetIndex < TARGET_COUNT; ++targetIndex) {
    //     float f_targetIndex = float(targetIndex);
    //
    //     float trigOffset = (PI / float(TARGET_COUNT)) * f_targetIndex;
    //
    //     vec3 targetPosition = vec3(sin(iTime + trigOffset) * 0.51 + sin(f_targetIndex + trigOffset), tan(iTime + trigOffset) * 0.1 + sin(f_targetIndex + trigOffset),
    //       cos(iTime + trigOffset) * 0.51 + cos(f_targetIndex + trigOffset));
    //     vec3 edgeBoundPosition = targetPosition / GRID_CELL_SIZE;
    //
    //     float distanceToTarget = length(gridBoundUV - round(edgeBoundPosition)) + distance(gridBoundUV, edgeBoundPosition);
    //     blueIntensity += length(GRID_CELL_SIZE / (distanceToTarget * 15.5 * cellBoundUV) ) * GRID_CELL_SIZE;
    // }
    //
    //
    // vec3 col = vec3(smoothstep(0.2, 1.0, redIntensity + blueIntensity));
    // col += redIntensity * GREEN;
    // col += blueIntensity * BLUE;
    
    // gl_FragColor = vec4(col, 1.0);
  }
  
`
}

export default RandomGridShader;
